/*
 * 
 * 
 * 
 */
package com.cms.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.domain.Feedback;
import com.cms.entity.Nav;
import com.cms.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Controller - 导航
 * 
 * 
 * 
 */
@Controller("adminNavController")
@RequestMapping("/admin/nav")
public class NavController extends BaseController {

	@Autowired
	private NavService navService;
	
	/**
	 * 列表
	 */
	@RequestMapping
	public String index(ModelMap modelMap) {
		setListQuery();
		modelMap.addAttribute("navTree", navService.findTree(getCurrentSite().getId()));
		return getView("nav/index");
	}

	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.addAttribute("navTree", navService.findTree(getCurrentSite().getId()));
		return getView("nav/add");
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(Nav nav) {
		nav.setSiteId(getCurrentSite().getId());
		nav.setValue();
		nav.setCreateDate(LocalDateTime.now());
		nav.setUpdateDate(LocalDateTime.now());
		navService.save(nav);
		return "redirect:"+getListQuery("/admin/nav");
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		Nav nav = navService.getById(id);
		modelMap.addAttribute("navTree", navService.findTree(getCurrentSite().getId()));
		modelMap.addAttribute("nav", nav);
		return getView("nav/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(Nav nav) {
		nav.setValue();
		nav.setUpdateDate(LocalDateTime.now());
		navService.updateById(nav);
		return "redirect:"+getListQuery("/admin/nav");
	}
	
   /**
     * 修改排序
     */
   	@ResponseBody
   	@RequestMapping("/updateSort")
    public Feedback updateSort(String sortArray){
        JSONArray jsonArray = JSONArray.parseArray(sortArray);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer id = jsonObject.getInteger("id");
            Integer sort = jsonObject.getInteger("sort");
            Nav nav = navService.getById(id);
			nav.setSort(sort);
			navService.updateById(nav);
        }
        return Feedback.success(new HashMap<>());
    }

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer id) {
		Nav nav = navService.getById(id);
		if (nav == null) {
			return Feedback.error("导航不存在");
		}
		List<Nav> children = nav.getChildren();
		if (children != null && !children.isEmpty()) {
			return Feedback.error("存在下级导航，无法删除");
		}
		navService.removeById(id);
		return Feedback.success(new HashMap<>());
	}

}